package com.ruoyi.wms.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsInventoryAllocationMapper;
import com.ruoyi.wms.domain.WmsInventoryAllocation;
import com.ruoyi.wms.service.IWmsInventoryAllocationService;

/**
 * 库存分配Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class WmsInventoryAllocationServiceImpl implements IWmsInventoryAllocationService 
{
    @Autowired
    private WmsInventoryAllocationMapper wmsInventoryAllocationMapper;

    /**
     * 查询库存分配
     * 
     * @param allocationId 库存分配主键
     * @return 库存分配
     */
    @Override
    public WmsInventoryAllocation selectWmsInventoryAllocationById(Long allocationId)
    {
        return wmsInventoryAllocationMapper.selectWmsInventoryAllocationById(allocationId);
    }

    /**
     * 查询库存分配列表
     * 
     * @param wmsInventoryAllocation 库存分配
     * @return 库存分配
     */
    @Override
    public List<WmsInventoryAllocation> selectWmsInventoryAllocationList(WmsInventoryAllocation wmsInventoryAllocation)
    {
        // 设置租户ID
        wmsInventoryAllocation.setTenantId(SecurityUtils.getTenantId());
        return wmsInventoryAllocationMapper.selectWmsInventoryAllocationList(wmsInventoryAllocation);
    }

    /**
     * 根据明细ID查询分配列表
     * 
     * @param detailId 出库单明细ID
     * @return 库存分配集合
     */
    @Override
    public List<WmsInventoryAllocation> selectWmsInventoryAllocationByDetailId(Long detailId)
    {
        return wmsInventoryAllocationMapper.selectWmsInventoryAllocationByDetailId(detailId);
    }

    /**
     * 根据出库单ID查询分配列表
     * 
     * @param orderId 出库单ID
     * @return 库存分配集合
     */
    @Override
    public List<WmsInventoryAllocation> selectWmsInventoryAllocationByOrderId(Long orderId)
    {
        return wmsInventoryAllocationMapper.selectWmsInventoryAllocationByOrderId(orderId);
    }

    /**
     * 新增库存分配
     * 
     * @param wmsInventoryAllocation 库存分配
     * @return 结果
     */
    @Override
    public int insertWmsInventoryAllocation(WmsInventoryAllocation wmsInventoryAllocation)
    {
        // 验证必需字段
        if (wmsInventoryAllocation.getOrderId() == null) {
            throw new RuntimeException("出库单ID不能为空");
        }
        if (wmsInventoryAllocation.getDetailId() == null) {
            throw new RuntimeException("出库明细ID不能为空");
        }
        if (wmsInventoryAllocation.getInventoryId() == null) {
            throw new RuntimeException("库存ID不能为空");
        }
        if (wmsInventoryAllocation.getWarehouseId() == null) {
            throw new RuntimeException("仓库ID不能为空");
        }
        
        // 设置租户ID和创建信息
        wmsInventoryAllocation.setTenantId(SecurityUtils.getTenantId());
        wmsInventoryAllocation.setCreateBy(SecurityUtils.getUsername());
        wmsInventoryAllocation.setCreateTime(DateUtils.getNowDate());
        wmsInventoryAllocation.setUpdateBy(SecurityUtils.getUsername());
        wmsInventoryAllocation.setUpdateTime(DateUtils.getNowDate());
        return wmsInventoryAllocationMapper.insertWmsInventoryAllocation(wmsInventoryAllocation);
    }

    /**
     * 修改库存分配
     * 
     * @param wmsInventoryAllocation 库存分配
     * @return 结果
     */
    @Override
    public int updateWmsInventoryAllocation(WmsInventoryAllocation wmsInventoryAllocation)
    {
        wmsInventoryAllocation.setUpdateBy(SecurityUtils.getUsername());
        wmsInventoryAllocation.setUpdateTime(DateUtils.getNowDate());
        return wmsInventoryAllocationMapper.updateWmsInventoryAllocation(wmsInventoryAllocation);
    }

    /**
     * 批量删除库存分配
     * 
     * @param allocationIds 需要删除的库存分配主键
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryAllocationByIds(Long[] allocationIds)
    {
        return wmsInventoryAllocationMapper.deleteWmsInventoryAllocationByIds(allocationIds);
    }

    /**
     * 删除库存分配信息
     * 
     * @param allocationId 库存分配主键
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryAllocationById(Long allocationId)
    {
        return wmsInventoryAllocationMapper.deleteWmsInventoryAllocationById(allocationId);
    }

    /**
     * 根据明细ID删除分配
     * 
     * @param detailId 出库单明细ID
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryAllocationByDetailId(Long detailId)
    {
        return wmsInventoryAllocationMapper.deleteWmsInventoryAllocationByDetailId(detailId);
    }

    /**
     * 根据出库单ID删除分配
     * 
     * @param orderId 出库单ID
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryAllocationByOrderId(Long orderId)
    {
        return wmsInventoryAllocationMapper.deleteWmsInventoryAllocationByOrderId(orderId);
    }

    /**
     * 批量新增库存分配
     * 
     * @param allocationList 库存分配列表
     * @return 结果
     */
    @Override
    public int batchInsertWmsInventoryAllocation(List<WmsInventoryAllocation> allocationList)
    {
        if (allocationList == null || allocationList.isEmpty()) {
            return 0;
        }
        
        // 设置租户ID和创建信息，并验证必需字段
        for (WmsInventoryAllocation allocation : allocationList) {
            // 验证必需字段
            if (allocation.getOrderId() == null) {
                throw new RuntimeException("出库单ID不能为空");
            }
            if (allocation.getDetailId() == null) {
                throw new RuntimeException("出库明细ID不能为空");
            }
            if (allocation.getInventoryId() == null) {
                throw new RuntimeException("库存ID不能为空");
            }
            if (allocation.getWarehouseId() == null) {
                throw new RuntimeException("仓库ID不能为空");
            }
            
            allocation.setTenantId(SecurityUtils.getTenantId());
            allocation.setCreateBy(SecurityUtils.getUsername());
            allocation.setCreateTime(DateUtils.getNowDate());
            allocation.setUpdateBy(SecurityUtils.getUsername());
            allocation.setUpdateTime(DateUtils.getNowDate());
        }
        
        return wmsInventoryAllocationMapper.batchInsertWmsInventoryAllocation(allocationList);
    }
}